<?php

/**
 * This is the model class for table "aula".
 *
 * The followings are the available columns in table 'aula':
 * @property integer $id
 * @property integer $numero
 * @property integer $turma_id
 * @property string $local
 * @property string $horario
 * @property string $data
 * @property string $fatores
 * @property string $observacao
 *
 * The followings are the available model relations:
 * @property Turma $turma
 * @property AvaliacaoDiaria[] $avaliacaoDiarias
 * @property Frequencia[] $frequencias
 */
class Aula extends CActiveRecord
{

	public static $plano_tipo = array(
			'Objetivo' => 'Objetivo',
			'Conteúdo' => 'Conteúdo',
			'Procedimento' => 'Procedimento',
			'Recurso' => 'Recurso',
			'Avaliação' => 'Avaliação',
	);

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'aula';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
				array('numero, turma_id, horario, data, local', 'required','message'=>'Campo {attribute} está vazio.'),
				array('numero, turma_id', 'numerical', 'integerOnly'=>true,'message'=>'Campo {attribute} deve conter apenas números.'),
				array('local, fatores, observacao', 'length', 'max'=>250),
				array('horario', 'length', 'max'=>45),
				array('data', 'safe'),
				// The following rule is used by search().
				// @todo Please remove those attributes that should not be searched.
				array('id, numero, turma_id, local, horario, data, dias, fatores, observacao', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'turma' => array(self::BELONGS_TO, 'Turma', 'turma_id'),
				'avaliacaoDiarias' => array(self::HAS_MANY, 'AvaliacaoDiaria', 'aula_id'),
				'frequencias' => array(self::HAS_MANY, 'Frequencia', 'aula_id'),
				'planos' => array(self::HAS_MANY, 'Plano', 'aula_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
				'id' => 'ID',
				'numero' => 'Número',
				'turma_id' => 'Turma',
				'local' => 'Local',
				'horario' => 'Horário',
				'data' => 'Data',
				'fatores' => 'Fatores Intercorrentes',
				'observacao' => 'Observação',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search($parameter)
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;
		$criteria->addCondition('turma_id = :turma_id');
		$criteria->params = array(':turma_id' => $parameter);

		$criteria->compare('id',$this->id);
		$criteria->compare('numero',$this->numero);
		$criteria->compare('turma_id',$this->turma_id);
		$criteria->compare('local',$this->local,true);
		$criteria->compare('horario',$this->horario,true);
		$criteria->compare('data',$this->data,true);
		$criteria->compare('fatores',$this->fatores,true);
		$criteria->compare('observacao',$this->observacao,true);

		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
		));
	}

	public function delete() {
		Plano::model()->deleteAllByAttributes(array('aula_id'=>$this->id));
		Frequencia::model()->deleteAllByAttributes(array('aula_id'=>$this->id));
		AvaliacaoDiaria::model()->deleteAllByAttributes(array('aula_id'=>$this->id));
	
		parent::delete();
	}


	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Aula the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
